Data Adaptor - Sample Code (DerbyDataAdaptor)

Data Adaptor - Sample Code (DerbyDataAdaptor)

The sample DataAdaptor in this example does the following:

 

The sample code demonstrates:

 

The sample code is for requirements such as:

Setup

This sample code needs the following to run:

About the Parents and Children rulebase

The rule is not relevant for this sample, it is the entities, attributes, and relationships that we are interested about (since the Data Adaptor needs to save and load them)

The Rule entities it uses are: Global, child, person.

The relationships are:

 

source relationship type target source text target text
person m-m child personschild childsparents
The attributes relevant to this example:

Global

 

Child

 

Person

About the DERBY Database

The DERBY SQL database is created by using create_db.sql which is located in examples\interview-engine\data-adaptor\src.

The Database tables are:

WD_CASE (represents the Global entity)
WD_CASE_ID (VARCHAR(255), PK)
SUN_SHINING(INT)

 

CHILD
CHILD_ID (INT, PK-IDENTITY)
WD_CASE_ID (VARCHAR(255),FK)
CHILD_NAME (VARCHAR(255))
EATING_ICECREAM(INT)

 

PERSON
PERSON_ID (INT, PK – IDENTITY)
PERSON_NAME(VARCHAR(255))

 

CHILD_PARENT
(represents the m:m relationship 'childsparents')
CHILD_ID (INT, PK and FK)
PARENT_ID (INT, PK and FK)

About the Derby Data Adaptor plugin

The sample plugin can do the following:

 

All the main methods of the DerbyDataAdaptor (listCases, load and save) use the methods for connecting and disconnecting to the DERBY database (connectDBObjects, closeDBObjects). Because those methods connect to the database, they must put the method logic in a try-catch block.

To setup this scenario:

  1. Create the SQL Database using create_db.sql which can be found in examples\interview-engine\data-adaptor\src
  2. Copy the rulebase .zip file (Parents and Children.zip) from examples\rulebases\compiled to the rulebase folder in Web Determinations (for example, <webroot>\WEB-INF\classes\rulebases)
  3. Copy and install the DerbyDataAdaptor.jar file (located in examples\interview-engine\data-adaptor) into Web Determinations; for more information, refer to Create a Plugin
  4. Copy the DERBY libraries to library folder in Web Determinations (for example, <webroot>\WEB-INF\lib)
  5. Ensure that DERBY is run in network server mode
  6. Run a Web Determinations Interview

How the DerbyDataAdaptor works

 

The DerbyDataAdaptor is triggered by the following actions (detailed in the Data Adaptor Plugin page), that is:

listCases()

The listCases() method is straightforward. It needs to retrieve all the available cases that can be 'loaded'. These cases are stored in the WD_CASE table, so this method simply queries the WD_CASE table, retrieves all the ID's, and returns them to be displayed in the 'Load' screen.

load()

The load() method builds the InterviewUserData to be returned with the following steps:

  1. The user is authenticated
  2. The method connects to the database
  3. Creates a new interview user data
  4. Retrieves the global record in WD_CASE table by using the case ID
  5. Gets the global interview entity instance in the created interview user data
  6. Sets the attribute ‘sun_shining’ in the global instance
  7. Retrieves all children from the CHILD table by using the case ID
  8. For each child retrieved in step 7;
    1. An interview entity instance is created for the child
    2. The ‘child_name’ and ‘eating_icecream’ are set in the child’s interview entity instance.
    3. The child’s interview entity instance containment parent is set to the global interview entity instance.
  9. The global interview entity instance containment for child entity is set to complete.
  10. Retrieves all people from the CHILD table by using the case ID
  11. For each person retrieved in step 10;
    1. An interview entity instance is created for the person
    2. The ‘person_name’ attribute is set in the person’s interview entity instance.
    3. The person’s entity instance containment parent is set to global interview entity instance.
  12. The global interview entity instance containment for person entity is set to complete.
  13. For each person interview entity instance created in step 11, the relationship ‘personschildren’ is set by adding the appropriate child interview entity instance/s using the table CHILD_PARENT.
  14. The database objects are closed and connection is terminated.

save()

The save() method accesses the Rulebase model data (Rulebase object) and the Session instance data (Session object) to save the current Interview user data. For this sample code, the global, child and person entities’ instances are saved.

Only base attributes for all entities are saved.

The save process is as follows:

  1. The user is authenticated
  2. The method connects to the database
  3. The global entity instance from the session is written in the WD_CASE table with fields WD_CASE_ID and SUN_SHINING .
  4. For each contained child entity instance in the global entity instance, the child entity instance is written in the CHILD table with fields WD_CASE_ID, CHILD_NAME and EATING_ICECREAM.
    Note: When a record is written in the CHILD table, the CHILD_ID is automatically generated.
  5. For each contained person entity instance in the global entity instance, the person entity instance is written in the PERSON table with fields WD_CASE_ID and PERSON_NAME
    Note: When a record is written in the PERSON table, the PERSON_ID is automatically generated.
  6. The m:m relationship ‘parentschildren’ is recorded in the table CHILD_PARENT by using the generated CHILD_ID field values in table CHILD and PERSON_ID field values in table PERSON.
  7. The database objects are closed and connection is terminated.

Source Code

To view the source code for the DerbyDataAdaptor sample, refer to examples/interview-engine/data-adaptor in the Java runtime zip file.